const showErrorToast = (res, defaultmsg) => {
  let msg = '';
  if (defaultmsg) {
    msg = defaultmsg;
  } else if (res && res.data && res.data.msg) {
    msg = res.data.msg;
  } else if (res && res.errorMessage) {
    msg = res.errorMessage;
  }
  const isString = typeof msg === 'string';
  if (!isString) {
    msg = JSON.stringify(isString);
  }
  dd.showToast({
    type: 'fail',
    content: msg,
    duration: 2000
  });
};
const formatPosotion = (value) => {
  const reg = /([0-9]+\.[0-9]{6})[0-9]*/;
  let str = value.toString();
  str = str.replace(reg, '$1');
  return str;
};
const handleGetImageBase64 = async (content, path, maxLength = 1920, quality = 0.8) => {
  try {
    const ctx = content.ctx;
    // 获取图片信息
    const compressimg = await dd.compressImage({
      filePaths: [path],
      compressLevel: 1
    });
    const info = await dd.getImageInfo({
      src: compressimg.filePaths[0]
    });
    console.log('图片信息', info);
    const {
      width,
      height
    } = info;
    // 绘制图片
    content.setData({
      width,
      height
    });
    ctx.drawImage(info.path, 0, 0, width, height);
    await new Promise((resolve) => ctx.draw(false, resolve));
    // 图片尺寸超出限制的最大值时，按比例缩小至限制内
    const scale = maxLength / Math.max(width, height, maxLength);
    const destWidth = width * scale;
    const destHeight = height * scale;
    // 转换为base64
    const base64 = await ctx.toDataURL({
      x: 0,
      y: 0,
      width,
      height,
      destWidth,
      destHeight,
      fileType: 'jpg',
      quality
    });
    // 返回结果
    return base64;
  } catch (error) {
    return '';
  }
};
export default {
  showErrorToast,
  formatPosotion,
  handleGetImageBase64
};